﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Core;
using DataAccess.Classes;

public partial class Controls_ProductsList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateControls();
        }
    }
    private void PopulateControls()
    {
        int howManyPages = 0;
        string categoryID = Request.QueryString["CategoryID"] ?? "";
        string page = Request.QueryString["Page"] ?? "1";

        string searchString = Request.QueryString["Search"] ?? "";

        string firstPageUrl = "";
        string pagerFormat = "";

        if (searchString != "")
        {
            dlProdList.DataSource = Product.Search(searchString, page, out howManyPages);
            dlProdList.DataBind();
            firstPageUrl = Link.ToSearch(searchString, "1");
            pagerFormat = Link.ToSearch(searchString, "{0}");
        }
        else if (categoryID != "")
        {
            dlProdList.DataSource = Product.InCategory(categoryID, page, out howManyPages);
            dlProdList.DataBind();
            firstPageUrl = Link.ToCategory(categoryID);
            pagerFormat = Link.ToCategory(categoryID, "{0}");
        }
        else
        {
            dlProdList.DataSource = Product.OnPromo(page,out howManyPages);
            dlProdList.DataBind();
            firstPageUrl = Link.ToCatalog("1");
            pagerFormat = Link.ToCatalog("{0}");
        }
        pagerTop.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
        pagerBottom.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
    }
}